using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;
using System.Data;
using System.Data.Common;
using System.IO;
using CoyoEden.DataStore;
using System.Text;
using System.Xml.Serialization;
using CoyoEden.Models;

namespace CoyoEden.Providers
{
	/// <summary>
	/// Generic Database PortalProvider
	/// </summary>
	public partial class DbPortalProvider : PortalProvider
	{
		#region member variables
		private string connStringName;
		private string tablePrefix;
		private string parmPrefix;

		private static string connStringNameStatic;
		private static string connStringStatic;
		private static object _SyncRoot = new object();
		private static DbProviderFactory _MyDbFactory;
		/// <summary>
		/// Gets an unsorted list of all Categories.
		/// </summary>
		public static DbProviderFactory MyDbFactory
		{
			get
			{
				if (_MyDbFactory == null)
				{
					lock (_SyncRoot)
					{
						if (_MyDbFactory == null)
						{
							_MyDbFactory = GetDbFactory();
						}
					}
				}

				return _MyDbFactory;
			}
		}
		#endregion

		#region helper methods
		private static DbProviderFactory GetDbFactory()
		{
			connStringStatic = ConfigurationManager.ConnectionStrings[connStringNameStatic].ConnectionString;
			string providerName = ConfigurationManager.ConnectionStrings[connStringNameStatic].ProviderName;
			return DbProviderFactories.GetFactory(providerName);
		}
		#endregion

		#region Base override
		/// <summary>
		/// Initializes the provider
		/// </summary>
		/// <param name="name">Configuration name</param>
		/// <param name="config">Configuration settings</param>
		public override void Initialize(string name, NameValueCollection config) {
			if (config == null) {
				throw new ArgumentNullException("config");
			}

			if (String.IsNullOrEmpty(name)) {
				name = "DbPortalProvider";
			}

			if (String.IsNullOrEmpty(config["description"])) {
				config.Remove("description");
				config.Add("description", "Generic Database Blog Provider");
			}

			base.Initialize(name, config);

			if (config["connectionStringName"] == null) {
				// default to CoyoEden
				config["connectionStringName"] = "CoyoEden";
			}
			connStringName = config["connectionStringName"];
			config.Remove("connectionStringName");

			if (config["tablePrefix"] == null) {
				// default
				config["tablePrefix"] = "mb_";
			}
			tablePrefix = config["tablePrefix"];
			config.Remove("tablePrefix");

			if (config["parmPrefix"] == null) {
				// default
				config["parmPrefix"] = "@";
			}
			parmPrefix = config["parmPrefix"];
			config.Remove("parmPrefix");

			// Throw an exception if unrecognized attributes remain
			if (config.Count > 0) {
				string attr = config.GetKey(0);
				if (!String.IsNullOrEmpty(attr))
					throw new ProviderException("Unrecognized attribute: " + attr);
			}

			//set connStringNameStatic
			connStringNameStatic = connStringName;
		}
		#endregion
	}
}
